<?php

/**
 * 呼入记录
 */
class CallinController extends Controller_Base
{
    /**
     * 首页 - 列表
     */
    public function indexAction()
    {
        $getPage = $this->getRequest()->getQuery('page', 1);
        $limit = $this->getRequest()->getQuery('limit', 10);
        $page = intval(trim($getPage)) <= 0 ? 1 : intval(trim($getPage));
        $talkType = $this->getRequest()->getQuery('talkType', '');
        $litigantName = $this->getRequest()->getQuery('litigantName', '');
        $mediatorId = $this->getRequest()->getQuery('mediatorId', '');
        $callPhone = $this->getRequest()->getQuery('callPhone', '');
        $caseId = $this->getRequest()->getQuery('caseId', '');
        $importRecordId = $this->getRequest()->getQuery('importRecordId', 0);
        $assetsId = $this->getRequest()->getQuery('assetsId', 0);
        $starttalkTime = $this->getRequest()->getQuery('starttalkTime', 0);
        $endtalkTime = $this->getRequest()->getQuery('endtalkTime', 0);
        $contractNo = $this->getRequest()->getQuery('contractNo', '');
        // where
        $search = $pageQuery=[];
        $where = $this->getWhere();

        array_push($where,"callType in (2,3)");

        if($mediatorId){//调解员
            array_push($where,"mediatorId = '{$mediatorId}' ");
        }
        if($callPhone){//通话号码
            array_push($where,"callPhone = '{$callPhone}'");
        }
        if($caseId){//按照案件号查询
            array_push($where,"caseId = '{$caseId}'");
        }
        if($talkType){//按照案件号查询
            array_push($where,"talkType = '{$talkType}'");
        }
        if($litigantName){
            array_push($where,"callName like '%{$litigantName}%'");
        }
        if($importRecordId){
            array_push($where,"importRecordId = '{$importRecordId}'");
        }
        if($assetsId){
            array_push($where,"assetsId = '{$assetsId}'");
        }
        if($starttalkTime){
            array_push($where,"talkTime >= '{$starttalkTime}'");
        }
        if($endtalkTime){
            array_push($where,"talkTime <= '{$endtalkTime}'");
        }
        if($contractNo){
            array_push($where,"contractNo = '{$contractNo}'");
        }


        $add_begin_end = trim($this->getRequest()->getQuery('add_begin_end', ''));

        $query['add_begin_end'] = $add_begin_end;
        $date_tmp =  explode(" 到 ",$add_begin_end);
        $begin_date = $date_tmp[0];
        $end_date = $date_tmp[1];
        if (!empty($begin_date) || !empty($end_date)) {
            $begin_date = $begin_date ? $begin_date . " 00:00:00" : "";
            $end_date = $end_date ? $end_date . " 23:59:59" : "";
        }
        if ($end_date) {
            $end_date = strtotime($end_date);
            array_push($where, "callTime <= {$end_date}");

        }
        if ($begin_date) {
            $begin_date = strtotime($begin_date);
            array_push($where, "callTime >= {$begin_date}");
        }
        // count
        $mediationOrganId = trim($this->getRequest()->getQuery('mediationOrganId', 0));
        $pageQuery['mediationOrganId'] = $mediationOrganId;
        $pageQuery['limit'] = $limit;
        $pageQuery['talkType'] = $talkType;
        $pageQuery['mediatorId'] = $mediatorId;
        $pageQuery['callPhone'] = $callPhone;
        $pageQuery['caseId'] = $caseId;
        $pageQuery['litigantName'] = $litigantName;
        $pageQuery['importRecordId'] = $importRecordId;
        $pageQuery['assetsId'] = $assetsId;
        $pageQuery['starttalkTime'] = $starttalkTime;
        $pageQuery['endtalkTime'] = $endtalkTime;
        $pageQuery['contractNo'] = $contractNo;

        $search['where'] = $where;

        $countFields = ['count(*) as total'];
        $countArr = Module_Mediate_CallRecord::getInstance()->getAllBySearch($countFields, $search);
        $total_num = $countArr[0]['total'];
        // lis
        $list = [];

        if (intval($total_num) > 0) {
            $fields = [];
            $search['limit'] = $limit;
            $search['start'] = ($page - 1) * $limit;
            $search['order'] = 'addTime desc';
            $list = Module_Mediate_CallRecord::getInstance()->getAllBySearch($fields, $search);

        }

        //赋值调解部门
        $this->getMediationOrgan();
        //赋值资产方
        $this ->getAssets();
        //assign
        SmartyAdapter::assign('curPageNum', $page);
        SmartyAdapter::assign('totalnum', $total_num);
        SmartyAdapter::assign('totalPage', ceil($total_num / $limit));
        SmartyAdapter::assign('page_query', empty($pageQuery) ? '' : http_build_query($pageQuery));
        SmartyAdapter::assign('query', $pageQuery);
        SmartyAdapter::assign('list', $list);
        SmartyAdapter::$templateName = 'callin/index';
    }




    /**
     * 数据导出
     */
    public function exportAction()
    {
        $talkType = $this->getRequest()->getQuery('talkType', '');
        $litigantName = $this->getRequest()->getQuery('litigantName', '');
        $mediatorId = $this->getRequest()->getQuery('mediatorId', '');
        $callPhone = $this->getRequest()->getQuery('callPhone', '');
        $caseId = $this->getRequest()->getQuery('caseId', '');

        $importRecordId = $this->getRequest()->getQuery('importRecordId', 0);
        $assetsId = $this->getRequest()->getQuery('assetsId', 0);
        $starttalkTime = $this->getRequest()->getQuery('starttalkTime', 0);
        $endtalkTime = $this->getRequest()->getQuery('endtalkTime', 0);
        // where
        $search = [];
        $where = $this->getWhere();
        array_push($where,"callType in (2,3)");
        if($mediatorId){//调解员
            array_push($where,"mediatorId = '{$mediatorId}' ");
        }

        if($litigantName){
            array_push($where,"callName like '%{$litigantName}%'");
        }
        if($caseId){//按照案件号查询
            array_push($where,"caseId = '{$caseId}'");
        }
        if($talkType){//按照案件号查询
            array_push($where,"talkType = '{$talkType}'");
        }

        if($importRecordId){
            array_push($where,"importRecordId = '{$importRecordId}'");
        }
        if($assetsId){
            array_push($where,"assetsId = '{$assetsId}'");
        }
        if($starttalkTime){
            array_push($where,"talkTime >= '{$starttalkTime}'");
        }
        if($endtalkTime){
            array_push($where,"talkTime <= '{$endtalkTime}'");
        }

        if($callPhone){//通话号码
            array_push($where,"callPhone = '{$callPhone}'");
        }

        $add_begin_end = trim($this->getRequest()->getQuery('add_begin_end', ''));

        $date_tmp =  explode(" 到 ",$add_begin_end);
        $begin_date = $date_tmp[0];
        $end_date = $date_tmp[1];
        if (!empty($begin_date) || !empty($end_date)) {
            $begin_date = $begin_date ? $begin_date . " 00:00:00" : "";
            $end_date = $end_date ? $end_date . " 23:59:59" : "";
        }
        if ($end_date) {
            $end_date = strtotime($end_date);
            array_push($where, "callTime <= {$end_date}");

        }
        if ($begin_date) {
            $begin_date = strtotime($begin_date);
            array_push($where, "callTime >= {$begin_date}");
        }
        $data = [];

        $search['order'] = 'addTime desc';
        $search['where']= $where;
        $record = Module_Mediate_CallRecord::getInstance()->getAllBySearch([], $search);
        $assets = array_column(
            Module_InfoBase_Assets::getInstance()->getAllBySearch(
                ['id','name'],
                [],
                true
            ),
            'name',
            'id');
        if (!empty($record)) {
            $this->getMediationOrgan();
            $mediationOrgan= SmartyAdapter::getTplVars('mediationOrgan');


            foreach($record as $k=>$v){
                $data[$k]['id'] = $v['id'];
                $data[$k]['mediationOrganId'] = $mediationOrgan[$v['mediationOrganId']]['name'];
                $data[$k]['assets'] = $v['assetsId'] ? $assets[$v['assetsId']] : '';
                $data[$k]['caseId'] = $v['caseId'];
                $data[$k]['callName'] = $v['callName'];
                $data[$k]['callPhone'] = substr($v['callPhone'],0,3).'****'.substr($v['callPhone'],6,4);

                $data[$k]['mediatorName'] = $v['mediatorName'];
                $data[$k]['callTime'] = date('Y-m-d H:i:s',$v['callTime']);
                $data[$k]['callEndTime'] = date('Y-m-d H:i:s',$v['callEndTime']);
                $data[$k]['talkTime'] = $v['talkTime'];
                $data[$k]['talkType'] = $v['talkType']==1?'接通':'未接通';

            }
        }
        $header = ['序号', '调解部门', '资产方','案件号', '当事人', '手机号', '调解员', '呼叫时间','结束时间','通话时长','通话状态'];




        Lib_XlsxExport::getInstance()->downExcel($data, $header, '呼入记录');
        exit;


    }


}
